package com.baitiaojun.common.enums.impl;

import cn.hutool.core.util.ArrayUtil;
import com.baitiaojun.common.constants.HttpStatus;
import com.baitiaojun.common.enums.Enums;

import java.util.Arrays;

/**
 * 系统异常
 */
public enum ServerExpEnums implements Enums {

    EXP_SQL_ILLEGAL_PARAM(HttpStatus.ERROR, "sql参数不合法"),
    EXP_SQL_ILLEGAL_KEYWORD(HttpStatus.ERROR, "sql关键字不合法"),
    EXP_RATE_LIMIT(HttpStatus.ERROR, "系统繁忙, 请稍后再试"),
    EXP_RATE_LIMIT_SERVICE_TROUBLE(HttpStatus.ERROR, "限流服务不可用, 请稍后再试"),
    EXP_OBJECT_IS_NULL(HttpStatus.ERROR, "对象实例为空"),
    EXP_INVOKE_LOG_SERVICE(HttpStatus.ERROR, "无法调用log服务"),
    EXP_REFLECT_INVOKE(HttpStatus.ERROR, "反射调用异常"),
    EXP_INSTANCE_RESPONSE(HttpStatus.ERROR, "无法实例化响应流"),
    EXP_OBJECT_CONVERT(HttpStatus.ERROR, "对象类型转换异常"),
    EXP_LOGIN_USER(HttpStatus.UNAUTHORIZED, "用户登陆认证失效, 请重新登录"),
    EXP_LOGIN_REPEAT(HttpStatus.UNAUTHORIZED, "用户已登陆, 请勿重复登录"),
    EXP_TOKEN_INVALID(HttpStatus.UNAUTHORIZED, "无效token"),
    EXP_ACCESS_DENIED(HttpStatus.FORBIDDEN, "没有权限，请联系管理员授权"),
    EXP_METHOD_NOT_SUPPORT(HttpStatus.BAD_METHOD, "请求方式不支持"),
    EXP_CAPTCHA_INVALID(HttpStatus.UNAUTHORIZED, "登陆验证码无效"),
    EXP_CAPTCHA_ERROR(HttpStatus.UNAUTHORIZED, "登陆验证码错误"),
    EXP_DESCRPT_PASSWORD(HttpStatus.ERROR, "密码解析失败"),
    EXP_BAD_DB_CONNECTION(HttpStatus.ERROR, "数据库连接初始化错误"),
    EXP_BAD_CREATE_DB(HttpStatus.ERROR, "创建租户数据库失败"),
    EXP_BAD_GRANT_DB(HttpStatus.ERROR, "数据库授权失败"),
    EXP_BAD_CLOSE_STATEMENT(HttpStatus.ERROR, "操作语句关闭失败"),
    EXP_BAD_CLOSE_CONNECTION(HttpStatus.ERROR, "连接关闭失败"),
    EXP_DROP_DB_ERROR(HttpStatus.ERROR, "删除数据库发生异常"),
    EXP_EXECUTE_DB_ERROR(HttpStatus.ERROR, "执行数据库操作时发生错误"),
    EXP_DML_SCRIPT_ERROR(HttpStatus.ERROR, "初始化用户数据脚本时出错"),
    EXP_PASS_DECRYPT_ERROR(HttpStatus.ERROR, "密码解析异常");

    private final Integer code;

    private final String name;

    ServerExpEnums(Integer code, String name) {
        this.code = code;
        this.name = name;
    }

    @Override
    public Enums valueOf(Integer code) {
        return ArrayUtil.firstMatch(item -> item.getCode().equals(code), values());
    }

    @Override
    public int[] array() {
        return Arrays.stream(values()).mapToInt(ServerExpEnums::getCode).toArray();
    }

    @Override
    public Integer getCode() {
        return code;
    }

    @Override
    public String getName() {
        return name;
    }
}
